package com.pengyang.netty.demo.util;

import java.util.List;

import org.msgpack.MessagePack;

import com.pengyang.netty.demo.pojo.IMMessage;
import com.pengyang.netty.demo.pojo.SelfDefinedProtocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;

/**
* @ClassName: SelfDefinedDecoder  
* @author peng.yang
* @date 2018年5月9日 下午1:21:33 
* @Description:  
 */
public class SelfDefinedDecoder extends MessageToMessageDecoder<ByteBuf> {

	@Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List<Object> out) throws Exception {
        final int length = msg.readableBytes();
        final  byte[] array = new byte[length];
        msg.getBytes(msg.readerIndex(),array,0,length);
        out.add(new MessagePack().read(array, IMMessage.class));
	}

}
